package com.estate.struts.actions;

import java.util.Date;
import java.util.List;

import com.estate.business.DocumentService;
import com.estate.business.LendDocumentService;
import com.estate.utils.CertificateCatagoryUtil;
import com.estate.vo.impl.DocumentVo;
import com.estate.vo.impl.LendDocumentVo;
import com.estate.vo.impl.LibraryVo;

public class LendingApprovalAction extends EstateSupport {

	private DocumentService documentService;
	private LendDocumentService lendDocumentService;
	
	private String method;
	private String checkId;
	private String lendId;
	
	public String execute() throws Exception{
		
		String page="";
		
		if("check".equals(method)){
			DocumentVo documentVo=lendDocumentService.findDocumentById(Integer.parseInt(this.getCheckId()));
			documentVo.setCatagoryName(CertificateCatagoryUtil.getCatagoryString(documentVo.getCatagory()));
			LibraryVo vo=documentVo.getLibrary();
			this.getSession().setAttribute("documentList", documentVo);
			this.getSession().setAttribute("libraryName", vo.getLibraryName());
			LendDocumentVo lendVo=lendDocumentService.findByPK(Integer.parseInt(this.getCheckId()));
			this.getSession().setAttribute("user", lendVo.getUser());
			this.getSession().setAttribute("applyDate", lendVo.getApplyDate());
			this.getSession().setAttribute("lendId", lendVo.getId());
			page="check";
		} else if("agree".equals(method)){
			LendDocumentVo vo=lendDocumentService.findByPK(Integer.parseInt(this.getLendId()));
			vo.setAuthorState(4);
			Date date=new Date(System.currentTimeMillis());
			vo.setAuthorDate(date);
			lendDocumentService.update(vo);
			DocumentVo documentVo=vo.getDocument();
			documentVo.setLendState(4);
			documentService.update(documentVo);
			page="refresh";
		} else if("disagree".equals(method)){
			LendDocumentVo vo=lendDocumentService.findByPK(Integer.parseInt(this.getLendId()));
			vo.setAuthorState(3);
			DocumentVo documentVo=vo.getDocument();
			documentVo.setLendState(3);
			documentService.update(documentVo);
			lendDocumentService.update(vo);
			page="refresh";
		} else if("list".equals(method)){
			List<LendDocumentVo> voList=lendDocumentService.findByState(2);
			this.getSession().setAttribute("lendDocument", voList);
			page="list";
		}
		return page;
	}

	public void setDocumentService(DocumentService documentService) {
		this.documentService = documentService;
	}

	public DocumentService getDocumentService() {
		return documentService;
	}

	public void setLendDocumentService(LendDocumentService lendDocumentService) {
		this.lendDocumentService = lendDocumentService;
	}

	public LendDocumentService getLendDocumentService() {
		return lendDocumentService;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public String getMethod() {
		return method;
	}

	public void setCheckId(String checkId) {
		this.checkId = checkId;
	}

	public String getCheckId() {
		return checkId;
	}

	public void setLendId(String lendId) {
		this.lendId = lendId;
	}

	public String getLendId() {
		return lendId;
	}
	
}
